package controller

import (
	"html/template"
	"net/http"
	"time"
)

func registerWeb() {
	http.HandleFunc("/", handleFunc)
	http.Handle("/css/", http.FileServer(http.Dir("wwwroot")))
	http.Handle("/img/", http.FileServer(http.Dir("wwwroot")))
}

func handleFunc(w http.ResponseWriter, r *http.Request) {

	if pusher, ok := w.(http.Pusher); ok {
		pusher.Push("/css/index.css", &http.PushOptions{
			Header: http.Header{"Content-Type": []string{"application/json"}},
		})
	}

	// t, err := template.ParseFiles("view/layout.html", "view/index.html")
	// err = t.ExecuteTemplate(w, "layout.html", time.Now())

	// t, err := template.ParseFiles("view/layout.html", "view/range.html")
	// err = t.ExecuteTemplate(w, "layout.html", []string{"面条", "米饭", "包子", "饭团"})

	// 设置类的action
	// t, err := template.ParseFiles("view/layout.html", "view/with.html")
	// err = t.ExecuteTemplate(w, "layout.html", "米饭")

	// 判断类的action
	// t, err := template.ParseFiles("view/layout.html", "view/if.html")
	// err = t.ExecuteTemplate(w, "layout.html", 1 == 1)

	t := template.New("context")
	t.Funcs(template.FuncMap{"dtime": dtime})
	t, err := t.ParseFiles("view/layout.html", "view/if.html")
	err = t.ExecuteTemplate(w, "layout.html", time.Now())

	if err != nil {

	}

}

func dtime(t time.Time) string {

	return t.Format("2006-01-02 15:04:05")
}
